/**
 * 
 */
package org.sevaapp.security.dao;

import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sevaapp.dao.DaoException;
import org.sevaapp.security.domain.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Srini
 *
 */
@Repository
@Transactional
public class UserDaoJpaImpl implements UserDao {

	private final Log log = LogFactory.getLog(UserDaoJpaImpl.class);

	private EntityManager em;

	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	public User load(String id) throws DaoException {
		return em.find(User.class, id);
	}

	public Collection<User> findAll() throws DaoException {
		String query = "SELECT user from users user";
		List<User> domainObjList = (List<User>) em.createQuery(query)
				.getResultList();
		return domainObjList;
	}

	public void add(User domainObject) throws DaoException {
		log.debug("add() method called.");
		em.persist(domainObject);
	}

	public void update(User domainObject) throws DaoException {
		em.merge(domainObject);
	}

	public void delete(String id) throws DaoException {
		User domainObj = em.find(User.class, id);
		em.merge(domainObj);

		// Now call remove method.	
		em.remove(domainObj);
	}

}
